
<template>
  <div class="task-detail">
    <div class="back-button-container">
      <button @click="goBackToHome" class="back-button">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
          <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
        </svg>
        返回首页
      </button>
    </div>
    
    <h2>任务详情</h2>
    
    <div v-if="task" class="task-card">
      <h3>{{ task.title }}</h3>
      <p><strong>描述:</strong> {{ task.description }}</p>
      <p><strong>截止日期:</strong> {{ task.dueDate }}</p>
      <p><strong>状态:</strong> <span class="status-tag">{{ task.completed ? '已完成' : '未完成' }}</span></p>
      
      <div class="task-actions">
        <button @click="toggleStatus" class="status-button">
          {{ task.completed ? '标记为未完成' : '标记为已完成' }}
        </button>
        <button @click="deleteTask">删除任务</button>
        <router-link to="/" class="back-link">返回首页</router-link>
      </div>
    </div>
    
    <div v-else class="loading">
      <p>加载任务中...</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useTaskStore } from '../store/taskStore';
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import type { Task } from '../types/task';

const taskStore = useTaskStore();
const route = useRoute();
const router = useRouter();

const taskId = route.params.id as string;
const task = ref<Task | null>(null);

const goBackToHome = () => {
  router.push('/');
};

onMounted(() => {
  const foundTask = taskStore.tasks.find((t: Task) => t.id === taskId) || null;
  
  if (foundTask) {
    task.value = foundTask;
  } else {
    router.push('/');
  }
});

const toggleStatus = () => {
  if (task.value) {
    taskStore.toggleTaskStatus(task.value.id);
  }
};

const deleteTask = () => {
  if (task.value) {
    taskStore.deleteTask(task.value.id);
    router.push('/');
  }
};
</script>

<style scoped>
.task-detail {
  margin-top: 2rem;
}

.back-button-container {
  margin-bottom: 1.5rem;
}

.back-button {
  display: flex;
  align-items: center;
  padding: 0.5rem 1rem;
  background-color: #f0f2ff;
  color: #4a6cf7;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background-color 0.3s;
}

.back-button svg {
  margin-right: 0.5rem;
}

.back-button:hover {
  background-color: #e0e6ff;
}

.status-tag {
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.875rem;
  font-weight: 500;
}

.status-tag[class*='status-'] {
  color: white;
}

.status-tag.status-completed {
  background-color: #67c23a;
}

.status-tag.status-pending {
  background-color: #e6a23c;
}

.task-card {
  background-color: #fff;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.task-actions {
  margin-top: 1rem;
  display: flex;
  gap: 1rem;
}

.status-button {
  padding: 0.5rem 1rem;
  background-color: #4a6cf7;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.status-button:hover {
  background-color: #3a5ce7;
}

button {
  padding: 0.5rem 1rem;
  background-color: #4a6cf7;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #3a5ce7;
}

.back-link {
  color: #4a6cf7;
  text-decoration: none;
  padding: 0.5rem 1rem;
  border: 1px solid #4a6cf7;
  border-radius: 4px;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-link:hover {
  background-color: #f0f2ff;
}

.loading {
  padding: 2rem;
  text-align: center;
  color: #999;
}
</style>